package com.taotao.web.service;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.taotao.common.service.HttpClientService;
import com.taotao.common.service.RedisService;
import com.taotao.web.bean.ItemDesc;
import com.taotao.web.bean.ItemVO;

@Service
public class ItemService {

	@Autowired(required = false) //注入HttpClient工具类
	private HttpClientService httpClientService;

	@Autowired(required = false) //注入加载外部资源文件的工具类
	private PropertiesService propertiesService;

	@Autowired
	private RedisService shardedJedisPoolService;

	public ItemVO queryItemById(Long itemId) throws ClientProtocolException, IOException {
		//0、查看缓存中是否已经有数据，命中缓存则直接返回，没有命中则查询数据库，最后将数据存储到缓存中
		//缓存机制一定不能影响程序正常运行，所以需要捕获异常
		String str = propertiesService.REDIS_ITEM_ID;
		String key = StringUtils.replace(str, "{ID}", itemId.toString());
		try {
			//获取缓存
			String redisResult = shardedJedisPoolService.get(key);
			if (StringUtils.isNotEmpty(redisResult)) {
				//命中缓存后转换成对象返回
				return PropertiesService.MAPPER.readValue(redisResult, ItemVO.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//1.HttpClient调用后台提供的接口
		//接口地址：String url="http://manage.taotao.com/rest/api/item"
		String url = propertiesService.TT_MANAGE_BASE_URL
				+ propertiesService.ITEM_PATH.replace("{itemId}", itemId.toString());//使用接收到的商品id进行替换
		String jsonDate = httpClientService.doGet(url);

		//2.返回数据之前，先将查询到的所有商品类目信息存储到缓存中

		try {
			int time = Integer.parseInt(propertiesService.REDIS_ITEM_ID_TIME);
			shardedJedisPoolService.setex(key, time, jsonDate);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//2.解析json格式数据,封装成Item对象
		ItemVO item = PropertiesService.MAPPER.readValue(jsonDate, ItemVO.class);

		//3.返回
		return item;
	}

	public ItemDesc queryItemDescById(Long itemId) throws JsonParseException, JsonMappingException, IOException {
		//0、查看缓存中是否已经有数据，命中缓存则直接返回，没有命中则查询数据库，最后将数据存储到缓存中
		//缓存机制一定不能影响程序正常运行，所以需要捕获异常
		String str = propertiesService.REDIS_ITEMDESC_ID;
		String key = StringUtils.replace(str, "{ID}", itemId.toString());
		try {
			//获取缓存
			String redisResult = shardedJedisPoolService.get(key);
			if (StringUtils.isNotEmpty(redisResult)) {
				//命中缓存后转换成对象返回
				return PropertiesService.MAPPER.readValue(redisResult, ItemDesc.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//1.HttpClient调用后台提供的接口
		//接口地址：String url="http://manage.taotao.com/rest/api/itemDesc"
		String url = propertiesService.TT_MANAGE_BASE_URL
				+ propertiesService.ITEMDESC_PATH.replace("{itemId}", itemId.toString());
		String jsonDate = httpClientService.doGet(url);

		//2.返回数据之前，先将查询到的所有商品类目信息存储到缓存中

		try {
			int time = Integer.parseInt(propertiesService.REDIS_ITEMDESC_ID_TIME);
			shardedJedisPoolService.setex(key, time, jsonDate);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//2.解析json格式数据,封装成ItemDesc对象
		ItemDesc itemDesc = PropertiesService.MAPPER.readValue(jsonDate, ItemDesc.class);

		//3.返回
		return itemDesc;
	}

}
